package com.hqj.demo;

import com.sun.deploy.panel.CacheSettingsDialog;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import java.util.SimpleTimeZone;

/**
 * Calendar的练习
 * @author HQJ
 * 利用java的日历Calendar 实现，获取当前系统时间，
 * 1、输出当前时间格式 2020-07-27  09:39:12 星期一
 *
 * 2、计算今天距离下一个国庆还有多少天？
 *
 * 3、在键盘输入一个月份数字，分别输出这个数字月份的第一天与最后一天是星期几？
 */
public class Exercise03 {

    public static void main(String[] args) {
        TimeUtil tu = new TimeUtil();
        System.out.println(tu.getNowTime());
        Scanner scan = new Scanner(System.in);
        tu.firstAndLastDay(scan.nextInt());
    }
}
class TimeUtil{

    public String getNowTime(){
        //创建日历对象
        Calendar c = Calendar.getInstance();

        int y = c.get(Calendar.YEAR);//年
        int m = c.get(Calendar.MONTH)+1;//月  从0开始  0-1 1-2
        int d = c.get(Calendar.DAY_OF_MONTH);//日

        int h = c.get(Calendar.HOUR);//小时  HOUR 12小时制  HOUR_OF_DAY  24小时制
        int mi = c.get(Calendar.MINUTE);//分钟
        int s = c.get(Calendar.SECOND);//秒

        int w = c.get(Calendar.DAY_OF_WEEK);
        String ws[] = {"天","一","二","三","四","五","六"};
        String str = y+"-"+addZero(m)+"-"+addZero(d)+"  "+addZero(h)+":"+addZero(mi)+":"+addZero(s)+" 星期"+ws[w-1];
        return str;
    }

    public void CalcDates() {
        try {

            String str = "10-01";
            Calendar c = Calendar.getInstance();
            int year = c.get(1);

            int month = c.get(2)+1;
            if(month>=10) year++;

            str = year + "-" + str;
            //把字符串转换为Date
            Date gq = new SimpleDateFormat("yyyy-MM-dd").parse(str);
            long gq_l = gq.getTime();
            long c_l = c.getTimeInMillis();

            long l = Math.abs(gq_l - c_l);
            long days = l/(24*60*60*1000);

            System.out.println("当前时间距离"+str+",还有"+days+"天");
        }catch (Exception e){
            e.printStackTrace();
        }


    }

    public void firstAndLastDay(int month){//10
        //创建日历对象
        Calendar c = Calendar.getInstance();
        //设置月份
        c.set(Calendar.MONTH,month-1);
        //设置日
        c.set(Calendar.DAY_OF_MONTH,1);

        int year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH)+1;
        SimpleDateFormat sdf = new SimpleDateFormat("E");
        System.out.println(year+"年"+month+"月的第一天:"+sdf.format(c.getTime()));

        //月份+1
        c.add(Calendar.MONTH,1);
        //天数-1
        c.add(Calendar.DAY_OF_MONTH,-1);
        System.out.println(year+"年"+month+"月的最后一天:"+sdf.format(c.getTime()));
    }


    /**
     * 补0
     * @param number
     * @return
     */
    public String addZero(int number){
        if(number<10){
            return "0"+number;
        }else{
            return number+"";
        }
    }
}
